package com.google.code.head2head.util;

import java.util.HashSet;
import java.util.Set;

import org.apache.commons.beanutils.BeanUtils;

import com.google.code.head2head.model.Entry;
import com.google.code.head2head.model.Grade;
import com.google.code.head2head.model.WebUser;

public class DtoConverter {
	
	public static com.google.code.head2head.gwtmodules.client.dto.Entry transform(Entry entry) throws Exception{
		com.google.code.head2head.gwtmodules.client.dto.Entry dto= new com.google.code.head2head.gwtmodules.client.dto.Entry();
		dto.setDate(entry.getDate());
		dto.setDescription(entry.getDescription());
		dto.setFistImageUrl(entry.getFistImageUrl());
		dto.setId(entry.getId());
		dto.setLanguage(entry.getLanguage());
		dto.setSecondImageUrl(entry.getSecondImageUrl());
		dto.setTitle(entry.getTitle());
		
		Set dtoGrades=new HashSet();
		for(Grade grade: entry.getGrades()){
			dtoGrades.add(transform(grade));
		}
		dto.setGrades(dtoGrades);
		if(entry.getUser()!=null){
			dto.setUser(transform(entry.getUser()));
		}
		return dto;
	}
	
	public static com.google.code.head2head.gwtmodules.client.dto.WebUser transform(WebUser user) throws Exception{
		com.google.code.head2head.gwtmodules.client.dto.WebUser dto = new com.google.code.head2head.gwtmodules.client.dto.WebUser();
		BeanUtils.copyProperties(dto, user);
		return dto;
	}
	
	public static com.google.code.head2head.gwtmodules.client.dto.Grade transform(Grade grade) throws Exception{
		com.google.code.head2head.gwtmodules.client.dto.Grade dto=new com.google.code.head2head.gwtmodules.client.dto.Grade();
		dto.setId(grade.getId());
		dto.setGrade(grade.getGrade());
		return dto;
	}

}
